<?php
//要访问的目标页面
$page_url = "http://httpbin.org/ip";

// 用户名密码认证(私密代理/独享代理)
$username   = "应用id";
$password   = "应用密码";
//api链接
$api_url = "https://api.xiaoxiangdaili.com/ip/get?appKey=应用id&appSecret=应用密码&cnt=1&wt=json";

$proxy = getProxy($api_url);

function getProxy($api_url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $api_url);

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

    curl_setopt($ch, CURLOPT_ENCODING, 'gzip'); //使用gzip压缩传输数据让访问更快

    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);

    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    $result = curl_exec($ch);
    curl_close($ch);

    $arr = json_decode($result, true);
    return $arr["data"][0]["ip"].":".$arr["data"][0]["port"];
}
echo $proxy;



$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $page_url);

//发送post请求
//$requestData["post"] = "send post request";
//curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($requestData));

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

//设置代理
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
curl_setopt($ch, CURLOPT_PROXY, "http://$proxy");
//设置代理用户名密码
curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, "{$username}:{$password}");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//自定义header
//$headers = array(
//    'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36'
//);
//curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//
////自定义cookie
//curl_setopt($ch, CURLOPT_COOKIE,'');
//
//curl_setopt($ch, CURLOPT_ENCODING, 'gzip'); //使用gzip压缩传输数据让访问更快
////
//curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
//curl_setopt($ch, CURLOPT_TIMEOUT, 10);
//
//curl_setopt($ch, CURLOPT_HEADER, true);
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);
curl_close($ch);

echo $result; // 使用请求页面方式执行时，打印变量需要加引号
